#!/bin/env ruby
# encoding: utf-8

require_relative "date"

def chek_standart_report_page_1
  
  a(href: '/en/us/profile').should be_present 
  select_list(id: 'js-report-selector-departments').should be_present
  input(class: "form-control input-sm reports-rangepicker").should be_present

  select_list(id: 'report-input-benefit').should be_present
  select_list(id: 'report-input-benefit')
    .option(text: 'Not sure').should be_present
  select_list(id: 'report-input-benefit')
    .option(text: 'Better strategic thinking').should be_present
  select_list(id: 'report-input-benefit')
    .option(text: "Increases customer satisfaction").should be_present
  select_list(id: "report-input-benefit")
    .option(text: "Increases innovation").should be_present
  select_list(id: "report-input-benefit")
    .option(text: "Increases productivity").should be_present
  select_list(id: "report-input-benefit")
    .option(text: "Increases profits").should be_present

  (browser.text.include? 'Download reports:').should be_truthy  
  (browser.text.include? 'Improvements').should be_truthy  
  (browser.text.include? 'completed over the period').should be_truthy  
  (browser.text.include? 'filtered by benefit').should be_truthy  
  (browser.text.include? 'over the period').should be_truthy  
  (browser.text.include? 'All users').should be_truthy  
  (browser.text.include? 'Improvement count per month since beginning per organization health').should be_truthy  
  (browser.text.include? 'Request count per month since beginning per engagement').should be_truthy 
  a(href: "/en/us/standard-report-users-csv/").should be_present
  a(href: "/en/us/standard-report-users-pdf/").should be_present
  a(href: "/en/us/standard-report-count-benefit-csv/").should be_present
  a(href: "/en/us/standard-report-count-benefit-pdf/").should be_present
  a(href: "/en/us/standard-report-count-category-csv/").should be_present
  a(href: "/en/us/standard-report-count-category-pdf/").should be_present
end

def chek_standart_report_page
  
  a(href: '/en/us/profile').should be_present 
  select_list(id: 'js-report-selector-departments').should be_present
  (inputs(class: "form-control input-sm reports-rangepicker").size()==3).should be_truthy

  selectList=select_list(id: 'report-input-benefit')
  selectList.should be_present
  selectList.option(text: 'Not sure').should be_present
  selectList.option(text: 'Better strategic thinking').should be_present
  selectList.option(text: "Increases customer satisfaction").should be_present
  selectList.option(text: "Increases innovation").should be_present
  selectList.option(text: "Increases productivity").should be_present
  selectList.option(text: "Increases profits").should be_present

  (browser.text.include? 'Download reports:').should be_truthy  
  (browser.text.include? 'Improvements').should be_truthy  
  (browser.text.include? 'completed over the period').should be_truthy  
  (browser.text.include? 'filtered by benefit').should be_truthy  
  (browser.text.include? 'over the period').should be_truthy  
  (browser.text.include? 'All users').should be_truthy  
  (browser.text.include? 'Improvement count per month since beginning per organization health')
  .should be_truthy  
  (browser.text.include? 'Request count per month since beginning per engagement')
  .should be_truthy  
end

def check_all_links
  selectList = select_list(id: "js-report-selector-departments")
    
  selectList.options.each do |option|  
    selectList.click()  
      selectList.select option.text()
      input(class: "form-control input-sm reports-rangepicker", index: num).click() 

      attribute=option.attribute_value('data-department-id')  
      # replace " " on "%20"       
      if attribute!=nil then         
        hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?departmentId=#{attribute.to_s}"
        hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?departmentId=#{attribute.to_s}"
        else
        hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}"
        hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}"
      end  
      STDERR.puts attribute.to_s
      STDERR.puts hrefCSV
      STDERR.puts hrefPDF
      STDERR.puts '**********************************************'
      #a(href: "#{hrefCSV.to_s}", text: 'CSV').should be_present
      #a(href: hrefPDF, text: 'PDF').should be_present
    end
end


def see_links
	STDERR.puts '########'
	browser.links.each do |link|
		STDERR.puts a.attribute_value("href")
	end
	STDERR.puts '########'
end

def check_interval(num,a,b)
  browser.element(:xpath, "html/body/div[#{num}]").should be_present 
  tables[a].wait_until_present
  tables[b].wait_until_present


  start_day=day(tables[a])
  end_day=day(tables[b])
  start_month=month(tables[a])
  end_month=month(tables[b])
  #check for non-empty interval 
  start_date=Hello::Date.new(start_day, start_month)
  end_date  =Hello::Date.new(end_day, end_month)  
  STDERR.puts "************************"
  STDERR.puts start_date.to_s()
  STDERR.puts "************************"
  STDERR.puts end_date.to_s()
  STDERR.puts "************************"

  return(start_date<=end_date) 

  # label(text: 'From').should be_present
  # label(text: 'To').should be_present
  # input(class: "input-mini", name: "daterangepicker_start").should  be_present
  # input(class: "input-mini", name: "daterangepicker_end").should  be_present
  # button(text: 'Apply').should be_present
  # button(text: 'Cancel').should be_present
  #browser.element(:xpath, "html/body/div[#{num}]/*/button[@text='Apply']").click()
  # browser.element(:xpath, "*/button[@text='Apply']").click()
  # browser.element(:xpath, "html/body/div[#{num}]/div[3]/div/button[1]").click()
  #return dates=[start_date, end_date]
end


def check_req_links()
  selectList = select_list(id: "report-input-author")
  optionsC=selectList.options
  optionsC.each do |opt|
  	attribute=opt.attribute_value('data-user-id')
    hrefCSV="/en/us/standard-report-improvements-author-csv/#{attribute.to_s}"
   	hrefPDF="/en/us/standard-report-improvements-author-pdf/#{attribute.to_s}" 

   	selectList.parent().focus()
   	#STDERR.puts  hrefCSV    
    #a(href: hrefCSV, text: 'CSV').should be_present    
    #STDERR.puts   hrefPDF
    #a(href: hrefPDF, text: 'PDF').should be_present  
  end   
end

def check_links(hrefA, start_date, end_date, num)
	selectList = select_list(id: "js-report-selector-departments")
	
    
	selectList.options.each do |option|	 
	  selectList.click()  
   	  selectList.select option.text()
   	  input(class: "form-control input-sm reports-rangepicker", index: num).click() 

   	  attribute=option.attribute_value('data-department-id')  
   	  # replace " " on "%20"    	 
   	  if attribute!=nil then   	  	 
  	    hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?departmentId=#{attribute.to_s}"
  	    hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?departmentId=#{attribute.to_s}"
  	    else
        hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}"
  	    hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}"
      end  
      STDERR.puts attribute.to_s
      STDERR.puts hrefCSV
      STDERR.puts hrefPDF
      STDERR.puts '**********************************************'
      #a(href: "#{hrefCSV.to_s}", text: 'CSV').should be_present
      #a(href: hrefPDF, text: 'PDF').should be_present
    end
end

def test_for_links(start_date, end_date)
  hrefA="/en/us/standard-report-improvements-on-benefit-"
  selectList = select_list(id: "report-input-benefit")
  deptId = select_list(id: "js-report-selector-departments").option
       .attribute_value('data-department-id')

  selectList.options.each do |option|	 
	  selectList.click()  
   	  selectList.select option.text()
   	  attribute=option.attribute_value('data-benefit-id')
      input(class: "form-control input-sm reports-rangepicker", index: 1).click()

      attr_str=attribute.tr_s(' ', '%20')
      hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?benefit=#{attr_str}&departmentId=#{deptId}"
      hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?benefit=#{attr_str}&departmentId=#{deptId}"
      
      STDERR.puts hrefCSV
      STDERR.puts hrefPDF
      a(href: "#{hrefCSV.to_s}").should be_present
      a(href: hrefPDF).should be_present
  end
end

def test1_for_links(start_date, end_date)
  hrefA="/en/us/standard-report-improvements-on-benefit-"
  
  select_list(id: "report-input-benefit").select 'Better strategic thinking'
  input(class: "form-control input-sm reports-rangepicker", index: 1).click()  
  hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?benefit=Better%20strategic%20thinking"
  hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?benefit=Better%20strategic%20thinking"
  a(href: hrefCSV).should be_present
  a(href: hrefPDF).should be_present

  select_list(id: "report-input-benefit").select "Increases customer satisfaction"
  input(class: "form-control input-sm reports-rangepicker", index: 1).click()  
  hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?benefit=Increases%20customer%20satisfaction"
  hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?benefit=Increases%20customer%20satisfaction"
  a(href: hrefCSV).should be_present
  a(href: hrefPDF).should be_present

  select_list(id: "report-input-benefit").select "Increases innovation"
  input(class: "form-control input-sm reports-rangepicker", index: 1).click()  
  hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?benefit=Increases%20innovation"
  hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?benefit=Increases%20innovation"
  a(href: hrefCSV).should be_present
  a(href: hrefPDF).should be_present

  select_list(id: "report-input-benefit").select "Increases productivity" 
  input(class: "form-control input-sm reports-rangepicker", index: 1).click()  
  hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?benefit=Increases%20productivity"
  hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?benefit=Increases%20productivity"
  a(href: hrefCSV).should be_present
  a(href: hrefPDF).should be_present

  select_list(id: "report-input-benefit").select "Increases profits"
  input(class: "form-control input-sm reports-rangepicker", index: 1).click()  
  hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?benefit=Increases%20profits"
  hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?benefit=Increases%20profits"
  a(href: hrefCSV).should be_present
  a(href: hrefPDF).should be_present	

  select_list(id: "report-input-benefit").select "Not sure"
  input(class: "form-control input-sm reports-rangepicker", index: 1).click()  
  hrefCSV=hrefA+"csv/#{start_date.to_s}/#{end_date.to_s}?benefit=Not%20sure"
  hrefPDF=hrefA+"pdf/#{start_date.to_s}/#{end_date.to_s}?benefit=Not%20sure"
  a(href: hrefCSV).should be_present
  a(href: hrefPDF).should be_present	
end

def day(table)   
  res_day=""
  day_exists = false
  table.tbody.trs.each do |row|
    row.cells.each do |cell|
      if cell.class_name == 'available active start-date end-date' then
        res_day = cell.text()
        day_exists = true                   
        break
      end
    end            
  end
  day_exists.should be_truthy
  return  res_day
end

def month(table)
  col_count=table.cols.count
  res_month=""
  month_exists = false
  table.thead.trs.each do |row|
    row.cells.each do |cell|
      if cell.class_name == 'month' then
        res_month = cell.text()
        month_exists = true                   
        break
      end
    end            
  end
  month_exists.should be_truthy
  return  res_month
end

def check_score_report_page
  a(href: '/en/us/profile').should be_present 
  
  (browser.text.include? "Engagement score report").should be_truthy  
  (browser.text.include? 'Filter by department (company by default):').should be_truthy  
  select_list(id: 'js-selector-departments').should be_present
  input(id: 'js-rangepicker',  class: 'form-control input-sm reports-rangepicker').should be_present
  
  svg.should be_present
  svg.tspan(text: 'How do you rate your relationship with your').should be_present
  svg.tspan(text: 'How do you rate your work and').should be_present
  svg.tspan(text: 'Are you recognized well enough?').should be_present
  svg.tspan(text: 'Do you have trust in leadership?').should be_present
  svg.tspan(text: 'Are you motivated to do your work?').should be_present
  svg.tspan(text: 'Are customers directly or indirectly happy because of your work?').should 
    be_present
  svg.tspan(text: 'Does your work directly or indirectly contribute to higher productivity?').should
    be_present
  svg.tspan(text: 'Does you work directly or indirectly contribute to of your team?').should be_present
  svg.tspan(text: 'Does your work directly or indirectly contribute to profitability?').should be_present
  svg.tspan(text: 'Does your work directly or indirectly contribute to ?').should be_present
end